//
//  Mine.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/18.
//

#import "Mine.h"
#import "MineCell.h"
#import "SelectPop.h"
#import "SelectOne.h"
#import "InfoVerify.h"
#import "BankVerify.h"
#import "Setting.h"
#import "Login.h"
#import "AppDelegate.h"
#import "TabBarViewController.h"
#import "DataFrameWeb.h"
#import "PopTool.h"
#import "CurrentVC.h"
@interface Mine ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIView *scrollContentView;
@property (nonatomic,strong)UILabel *name;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *source;
@property (nonatomic,strong)NSMutableDictionary *item;
@property (nonatomic,strong)NSMutableDictionary *info;
@end
#define Mine_Menu_Data @[@{@"title":@"Privacy Agreement",@"img":@"mine_set1"},@{@"title":@"Set Up",@"img":@"mine_set2"},@{@"title":@"Feedback",@"img":@"mine_set3"}]

@implementation Mine

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgIV.image = [UIImage imageNamed:@"mine_bg"];
    [self.view addSubview:bgIV];
    
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectZero];
    scroll.backgroundColor = UIColor.clearColor;
    [self.view addSubview:scroll];
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.mas_offset(64+MarginTop);
    }];
    _scrollView = scroll;
    [_scrollView addSubview:self.scrollContentView];
    [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    UIImageView *icon = [UIImageView new];
    icon.image = [UIImage imageNamed:@"mine_header"];
    [_scrollContentView addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.centerX.mas_equalTo(_scrollContentView);
    }];
    
    [_scrollContentView addSubview:self.name];
    [_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(icon.mas_bottom).offset(9);
        make.centerX.mas_equalTo(_scrollContentView);
    }];
    
    UIImageView *banner = [UIImageView new];
    banner.image = [UIImage imageNamed:@"mine_banner"];
    [_scrollContentView addSubview:banner];
    [banner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_name.mas_bottom).offset(25);
        make.left.mas_offset(16);
        make.right.mas_offset(-16);
        make.height.mas_equalTo(104);
    }];
    
    UIView *view = [UIView new];
    [_scrollContentView addSubview:view];
    view.backgroundColor = UIColor.clearColor;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(banner.mas_bottom).offset(-45);
    }];
    
    UIImageView *bg = [UIImageView new];
    bg.image = [UIImage imageNamed:@"mine_mid_bg"];
    [view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
        make.height.mas_equalTo(500);
    }];
    
    CGFloat width = (SCREEN_WIDTH -42)/2;
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"mine_service"] forState:UIControlStateNormal];
    [view addSubview:left];
    [left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.top.mas_offset(40);
        make.size.mas_equalTo(CGSizeMake(width, 70));
    }];
    
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    [right setImage:[UIImage imageNamed:@"mine_aboout"] forState:UIControlStateNormal];
    [view addSubview:right];
    [right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.top.mas_offset(40);
        make.size.mas_equalTo(CGSizeMake(width, 70));
    }];

    
    UILabel *tips = [UILabel new];
    tips.text = @"Common Tools";
    tips.textColor = HexColor(@"#333333");
    tips.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [view addSubview:tips];
    [tips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.top.equalTo(left.mas_bottom).offset(40);
    }];
    
    UIImageView *line = [UIImageView new];
    line.image = [UIImage imageNamed:@"line"];
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tips.mas_left);
        make.right.equalTo(tips.mas_right);
        make.top.equalTo(tips.mas_bottom).offset(-4);
        make.height.mas_equalTo(4);
    }];
    
    [view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(line.mas_bottom).offset(10);
    }];
    
    [left addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside];
    [right addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(logout)
                                                 name:@"logout"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(gotoAgree)
                                                 name:@"gotoAgree"
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginscuess)
                                                 name:@"loginscuess"
                                               object:nil];

}
-(void)loginscuess
{
        [self mineSource];
}
-(void)gotoAgree{
    NSInteger index = self.tabBarController.selectedIndex;
    if (index == 2) {
        NSString *privacyAgreement = [[NSUserDefaults standardUserDefaults] objectForKey:@"privacyAgreement"];
        if (![CPS(privacyAgreement) isEqualToString:@""]) {
            DataFrameWeb *web = [DataFrameWeb new];
            web.frameTo = @"https://spi-technologies.com/CPPrivacy.html";
            web.name = @"Privacy Agreement";
            web.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:web animated:YES];
        }
    }
}
-(void)logout
{
    
    _name.text = @"Hi~";
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    TabBarViewController* vc =  delegate.root;
    [vc selectIndex:0];

}

-(void)left{
    NSString *stewardess = [[NSUserDefaults standardUserDefaults] objectForKey:@"stewardess"];
    if([CPS(stewardess) isEqualToString:@""]){
        Login *login = [Login new];
        [self presentViewController:login animated:YES completion:nil];
        return ;
    }

    NSArray *arr = self.info[@"customerService"];
    NSDictionary *dict = arr[0];
    DataFrameWeb *web = [DataFrameWeb new];
    web.frameTo = CPS(dict[@"stop"]) ;
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}
-(void)right{
    NSString *stewardess = [[NSUserDefaults standardUserDefaults] objectForKey:@"stewardess"];
    if([CPS(stewardess) isEqualToString:@""]){
        Login *login = [Login new];
        [self presentViewController:login animated:YES completion:nil];
        return ;
    }
    NSDictionary *dict = self.source[0];
    if ([CPS(dict[@"stop"])  containsString:@"http"]) {
        DataFrameWeb *web = [DataFrameWeb new];
        web.name = CPS(dict[@"supposing"]) ;
        web.frameTo = CPS(dict[@"stop"]);
        web.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:web animated:YES];
    }else{
        Setting *vc = [Setting new];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [self mineSource];
}
-(void)mineSource{
    [SCRequest getInitWithUrl:@"/cham/drug" params:@{} isMsg:YES success:^(id  _Nonnull responseObject) {
        self.source = [NSMutableArray arrayWithArray:responseObject[@"fashion"][@"chieftain"]];
        self.info = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"fashion"]];
        self->_name.text = [NSString stringWithFormat:@"Hi~ %@",CPS(self.info[@"userInfo"][@"userphone"])];;
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
-(NSMutableDictionary *)item
{
    if (!_item) {
        _item =[NSMutableDictionary dictionary]
        ;    }
    return _item;
}
-(UIView *)scrollContentView{
    if (!_scrollContentView) {
        _scrollContentView = [UIView new];
        _scrollContentView.backgroundColor = UIColor.clearColor;
    }
    return _scrollContentView;
}
-(UILabel *)name
{
    if (!_name) {
        _name = [UILabel new];
        _name.textColor = HexColor(@"#212121");
        _name.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
    }
    return _name;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.bounces = NO;
    }
    return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.source.count - 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}
-(NSMutableArray *)source
{
    if (!_source) {
        _source = [NSMutableArray array];
    }
    return _source;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MineCell class])];
    if (cell == nil) {
        cell = [[MineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([MineCell class])];
    }
    cell.item = self.source[indexPath.row + 2] ;
    cell.backgroundColor = UIColor.clearColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *stewardess = [[NSUserDefaults standardUserDefaults] objectForKey:@"stewardess"];
    if([CPS(stewardess) isEqualToString:@""]){
        Login *login = [Login new];
        [self presentViewController:login animated:YES completion:nil];
        return ;
    }
    
    NSDictionary *dict = self.source[indexPath.row + 2];
    [self goDetail:dict];
    
}
-(void)oneTest{
    NSDictionary *params = @{@"gossip":@"1"};
    [SCRequest postHUDWithUrl:@"/cham/reckless" params:params isMsg:YES success:^(id  _Nonnull responseObject) {
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

-(void)goDetail:(NSDictionary *)dict
{
    NSString *str = CPS(dict[@"stop"]) ;
    if ([str containsString:@"http"]) {
        DataFrameWeb *web = [DataFrameWeb new];
        web.name = CPS(dict[@"supposing"]) ;
        web.frameTo = str;
        web.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:web animated:YES];
    }else{
        if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasouta"]) {
            Setting *vc = [Setting new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoutb"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:0];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoutc"]) {
            Login *login = [Login new];
            [self presentViewController:login animated:YES completion:nil];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoutd"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:1];
            NSArray *item = [str componentsSeparatedByString:@"="];
            NSString *tab = item[1];
            NSDictionary *userInfo = @{@"tab": tab};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoOrder" object:self userInfo:userInfo];
        }else if ([str containsString:@"jxe://ilgb.bbh.bjwv/wasoute"]) {
            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            TabBarViewController* vc =  delegate.root;
            [vc selectIndex:0];
            NSArray *item = [str componentsSeparatedByString:@"="];
            NSString *pid = item[1];
            NSDictionary *userInfo = @{@"pid": pid};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoDetail" object:self userInfo:userInfo];
        }
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
